import Base64 from './upload/base64'
import Crypto from './upload//crypto'
// import OSS from 'ali-oss'

const upload = {}
const accessid = 'LTAIvlI7R0Pfh1Jz';
const accesskey = 'cY78p5ovvTZmlSn1krpvrLHPOThykn';
// const host = 'https://mqins-bucket.oss-cn-zhangjiakou.aliyuncs.com';
const expirationTime = '2021-01-01T12:00:00.000Z'//上传文件时Policy的失效时间
const getPolicyBase64 = function () {
    let policyText = {
        "expiration": expirationTime, //设置该Policy的失效时间
        "conditions": [
            ["content-length-range", 0, 5 * 1024 * 1024] // 设置上传文件的大小限制,5mb
        ]
    }
    let policy = Base64.encode(JSON.stringify(policyText));
    return policy
}
const getSignature = function () {
    let message = getPolicyBase64();
    let bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, { asBytes: true });
    return Crypto.util.bytesToBase64(bytes);
}
upload.getFileNamesWithUID = (files) => {
    // 文件名称 uid.jpg
    let fileNames = []
    files.forEach(obj => {
        let file = obj.file
        let fileArray = file.name.split('.')
        let fileId = file.lastModified//file.uid
        let extendType = fileArray[1]
        let fileName = fileId + '.' + extendType
        fileNames.push(fileName)
    });
    return fileNames
}
upload.uploadFiles = (fileList, ossPath) => {
    //采用常规的postObject直传到oos
    let promises = [];
    let policy = getPolicyBase64();
    let signature = getSignature();
    fileList.forEach(item => {
        let fileName = upload.getFileNamesWithUID([item])[0];
        let fileKey = ossPath + fileName
        let formData = new FormData();
        formData.append('key', fileKey);
        formData.append('policy', policy)
        formData.append('OSSAccessKeyId', accessid)
        formData.append('success_action_status', '200')
        formData.append('signature', signature)
        formData.append('file', item.file)
        let promise = new promise((resolve, reject) =>{
          wx.uploadFile({
            url: 'http://localhost:9528/oss', //这个需要改
            filePath: item.file,
            name: 'file',
            formData: formData,
            success(res) {
              resolve(res);
            },
            fail(err) {
              reject(err);
            },
          })
        });
        promises.push(promise);
    })
    return Promise.all([promises]);

    // // 采用Oss  分片直传
    // let promises = [];
    // let client= new OSS({
    //     accessKeyId: accessid,
    //     accessKeySecret: accesskey,
    //     bucket: 'mqins-bucket',
    //     region: 'oss-cn-beijing'
    //   });
    //   fileList.forEach(item => {
    //     let fileName = upload.getFileNamesWithUID([item])[0];
    //     let fileKey = ossPath + fileName
    //     let promise =  client.multipartUpload(ossPath + fileName, item.raw, {
    //         progress: async function (p) {
    //           let e = {}
    //           e.percent = p * 100
    //              console.log(p)
    //         }
    //       })
    //       promises.push(promise);
    //   });
    // return Promise.all([promises]);
}
export default upload;